package com.frogs.ucenter.controller;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.entity.UserIntegralRecord;
import com.frogs.ucenter.entity.UserIntegralRecordWithUserResult;
import com.frogs.ucenter.service.UserIntegralRecordService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/admin/userIntegralRecord")
public class UserIntegralRecordController extends BaseController3 {
    private static final String ROOT_VIEW = "admin/userIntegralRecord";

    private static final String HOME_VIEW = "admin/userIntegralRecord/home";
    
    private static final String LIST_VIEW = "admin/userIntegralRecord/list";

    private static final String EDIT_VIEW = "admin/userIntegralRecord/edit";
    
    private static final String LIST = "/list";

    @Autowired
    private UserIntegralRecordService userIntegralRecordService;

    @RequestMapping()
    public String listUserIntegralRecord(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getPageListExt( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createUserIntegralRecord(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer integralId = reqInfo.getIntParameter( "integralId", -1 );
        if( integralId > 0 ){
            model.put( "model",userIntegralRecordService.getById( integralId ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveUserIntegralRecord(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserIntegralRecord userIntegralRecord) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder;
        try {
            userIntegralRecordService.insert( userIntegralRecord ); 
            builder = DWZResponse.getSucessBuilder("新增成功");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("新增失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        return null;
    }

    @RequestMapping(SHOW)
    public String showUserIntegralRecord(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer integralId = reqInfo.getIntParameter( "integralId", -1 );
        if( integralId > 0 ){
            model.addAttribute( userIntegralRecordService.getById( integralId ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateUserIntegralRecord(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserIntegralRecord userIntegralRecordNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer integralId = reqInfo.getIntParameter( "integralId", -1 );
        if( integralId > 0 ){
            DWZResponse.Builder builder;
            try {
                UserIntegralRecord userIntegralRecord = userIntegralRecordService.getById( integralId ); 
                PropertyUtils.copyProperties(userIntegralRecord, userIntegralRecordNew);
                userIntegralRecordService.update( userIntegralRecord ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("更新成功");
            } catch (Exception e) {
                builder = DWZResponse.getFailBuilder("更新失败" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        return null;
    }

    @RequestMapping(DELETE)
    public String deleteUserIntegralRecord(HttpServletRequest request, HttpServletResponse response, ModelMap model, String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            userIntegralRecordService.delete( model ); 
            builder = DWZResponse.getSucessBuilder("删除信息成功");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("删除信息失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        return null;
    }

    private PageList<UserIntegralRecord> getPageList(ModelMap model) {
    	model.put("orderByClause", " integral_time desc " );
        PageList<UserIntegralRecord> userIntegralRecordList = userIntegralRecordService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("userIntegralRecordList",userIntegralRecordList);
        model.put("pageTurn",userIntegralRecordList.getPageTurn());
        return userIntegralRecordList;
    }
    
    /**
     * 用户查询积分列表
     * 
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(LIST)
    public String listUserIntegralInfo(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getPageListExt( model );
        return LIST_VIEW;
    }
    
    private PageList<UserIntegralRecordWithUserResult> getPageListExt(ModelMap model) {
    	model.put("orderByClause", " a.integral_time desc " );
        PageList<UserIntegralRecordWithUserResult> userIntegralRecordList = 
        		userIntegralRecordService.listRecharge(model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()));
        model.put("userIntegralRecordList",userIntegralRecordList);
        model.put("pageTurn",userIntegralRecordList.getPageTurn());
        return userIntegralRecordList;
    }
}